/***************************************************************************
   Filename:    Paddle.cpp
   Authors:     Josh Partridge and Joe Flowers
   Description: Contains the Paddle class
****************************************************************************/

#include "Paddle.h"

/************************************************************************************
 Name:               Paddle - default constructor
 Description:        initialize attributes of Ball
*************************************************************************************/
Paddle::Paddle()
{
    setPosition(50);
    max_vel = 5;
    velocity = 0;
}

/************************************************************************************
 Name:               setPosition
 Description:        moves bounds RECT based on left side x position
 Input:
                  x: int representing new x position of left bound
*************************************************************************************/
void Paddle::setPosition(int x)
{
    bounds.left = x;
    bounds.right = x+16;
    bounds.top = 33;
    bounds.bottom = 34;
}

/************************************************************************************
 Name:               getPosition
 Description:        getter left side x position
 Output:
             return: integer representing left side x position
*************************************************************************************/
int Paddle::getPosition()
{
    return bounds.left;
}

/************************************************************************************
 Name:               setVelocity
 Description:        setter for velocity
 Input:
                  v: int representing new velocity
*************************************************************************************/
void Paddle::setVelocity(int v)
{
    velocity = v;

    //cap velocity
    if(velocity>max_vel)
        velocity = max_vel;
    else if(velocity< max_vel*-1)
        velocity = max_vel*-1;
}

/************************************************************************************
 Name:               getVelocity
 Description:        getter velocity
 Output:
             return: integer representing velocity
*************************************************************************************/
int Paddle::getVelocity()
{
    return velocity;
}

/************************************************************************************
 Name:               setAcc
 Description:        setter for acceleration, parameter depends on which key pressed
 Input:
                  a: int representing new acceleration
*************************************************************************************/
void Paddle::setAcc(int a)
{
    acceleration = a;
}

/************************************************************************************
 Name:               redraw
 Description:        applies velocity to position and draws, doesn't draw if the
                     paddle doesn't or can't move.
 Input:
             brush: reference to Game's pointer to main's brush
*************************************************************************************/
void Paddle::redraw(Brush& brush)
{
    //slow the paddle down if no arrow is being pressed (friction)
    if(acceleration == 0 && velocity > 0)
    {
        velocity --;
    }
    else if(acceleration == 0 && velocity < 0)
    {
        velocity ++;
    }

    //apply acceleration using setVelocity, which caps it at max_vel
    setVelocity(velocity+acceleration);

    //correct any clipping
    if(bounds.right+velocity>=116)
    {
        acceleration = 0;
        setPosition(99);
        //we need to draw before we reset the velocity
        draw(brush);
        velocity=0;
    }
    else if(bounds.left+velocity<=3)
    {
        acceleration = 0;
        setPosition(4);
        //we need to draw before we reset the velocity
        draw(brush);
        velocity=0;
    }else if(velocity!=0){
        setPosition(bounds.left+velocity);
        draw(brush);
    }
}

/************************************************************************************
 Name:               draw
 Description:        called by draw when it is ready to actally draw
 Input:
             brush: reference to Game's pointer to main's brush
             force: bool representing whether to not check velocity or validate position
*************************************************************************************/
void Paddle::draw(Brush& brush, bool force)
{
    brush.setColor(white);
    int density = 3;
    if(GetAsyncKeyState(VK_SPACE))
    {
        density=4;
    }
    else if(abs(velocity) == max_vel)
    {
        density =1;
    }
    else if(abs(velocity) >= max_vel/2)
    {
        density =2;
    }
    if(velocity>0 && bounds.right<=115)
    {
        brush.clearBox(bounds.left-velocity, bounds.top, velocity, 2);
        //solid blocks
        fancyFill(brush, bounds.left, bounds.top, bounds.right-bounds.left+1, 1, 0, density);
    }
    else if (velocity < 0 && bounds.left>=4)
    {
        brush.clearBox(bounds.right+1, bounds.top, velocity*-1, 2);
        fancyFill(brush, bounds.left, bounds.top, bounds.right-bounds.left+1, 1, 0, density);
    }
    else if(force){
        fancyFill(brush, bounds.left, bounds.top, bounds.right-bounds.left+1, 1, 0, density);
    }
}

